package com.example.leoss.denglu_zhuce;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.leoss.R;

import static org.apache.xmlbeans.impl.schema.StscState.start;

public class ZhuceActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText zhuce_ed1;
    private Button zhuce_bt1;
    private EditText zhuce_ed2;
    private EditText zhuce_ed3;
    private Button zhuce_bt2;
    private int r;

    private MyOkHttp myOkHttp;

    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    zhuce_bt1.setEnabled(false);
                    zhuce_bt1.setText("还剩"+msg.obj+"秒");
                    break;
                case 1:
                    zhuce_bt1.setEnabled(true);
                    zhuce_bt1.setText("发送验证码");
                    break;
                case 2:
                    Toast.makeText(ZhuceActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        initView();
    }

    private void initView() {
        zhuce_ed1 = (EditText) findViewById(R.id.zhuce_ed1);
        zhuce_bt1 = (Button) findViewById(R.id.zhuce_bt1);
        zhuce_ed2 = (EditText) findViewById(R.id.zhuce_ed2);
        zhuce_ed3 = (EditText) findViewById(R.id.zhuce_ed3);
        zhuce_bt2 = (Button) findViewById(R.id.zhuce_bt2);

        zhuce_bt1.setOnClickListener(this);
        zhuce_bt2.setOnClickListener(this);

        myOkHttp =new MyOkHttp(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhuce_bt1:
                final String ed1 = zhuce_ed1.getText().toString().trim();
                if (TextUtils.isEmpty(ed1)) {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                if (ed1.length()!=11){
                    Toast.makeText(this, "手机号输入不正确", Toast.LENGTH_SHORT).show();
                    break;
                }




                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        r=random();
                        final String s=myOkHttp.send(ed1,r+"");


                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ZhuceActivity.this, s, Toast.LENGTH_SHORT).show();
                            }
                        });
                        for (int i=60;i>0;i--){
                            try {
                                handler.obtainMessage(0,i).sendToTarget();
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        handler.obtainMessage(1).sendToTarget();
                    }
                }).start();

                break;
            case R.id.zhuce_bt2:
                submit();
                break;
        }
    }

    private int random(){
        int a=(int)(Math.random()*10000);
        return a>999 && a<10000 ? a : random();
    }

    private void submit() {
        // validate
        final String ed1 = zhuce_ed1.getText().toString().trim();
        if (TextUtils.isEmpty(ed1)) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String ed2 = zhuce_ed2.getText().toString().trim();
        if (TextUtils.isEmpty(ed2)) {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        final String ed3 = zhuce_ed3.getText().toString().trim();
        if (TextUtils.isEmpty(ed3)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (ed2.equals(r+"") && ed1.length()==11 && ed3.length()>6 && ed3.length()<20){
     new Thread(new Runnable() {
         @Override
         public void run() {
             String s=myOkHttp.zhuCe(ed1,ed3);
             handler.obtainMessage(2,s).sendToTarget();
         }
     }).start();

        }else {
            Toast.makeText(this, "错误", Toast.LENGTH_SHORT).show();
        }
    }
}
